package com.example.dialog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "msg";
    private static String xb;
    private static ArrayList<String> intrest = new ArrayList<>();
    private final int PROGRESS_DIALOG = 1;
    private final int INCREASE = 0;
    private ProgressDialog virtualProgressDialog = null;
    private Handler handler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what) {
                    case INCREASE:
                        virtualProgressDialog.incrementProgressBy(1);
                        if (virtualProgressDialog.getProgress() >= 100) {
                            virtualProgressDialog.dismiss();
                        }
                        break;
                }
                super.handleMessage(msg);
            }
        };
    }


    /**
     * 显示普通对话框
     *
     * @param view
     */
    public void showRDialog(View view) {
        rDialog();

    }

    /**
     * 显示单选对话框
     *
     * @param view
     */
    public void showRadioDialog(View view) {
        radioDialog();
    }

    /**
     * 显示多选对话框，并动态显示所选项，最后将选择的项目打印到logcat中
     *
     * @param view
     */
    public void showCheckBoxDialog(View view) {
        checkBoxDialog();
    }

    /**
     * 显示模拟进度条对话框
     *
     * @param view
     */
    public void showVirtualProgressDialog(View view) {
        showDialog(PROGRESS_DIALOG);
    }

    /**
     * 显示自定义对话框
     *
     * @param view
     */
    public void shouMyDialog(View view) {
        MyDialog myDialog = new MyDialog(MainActivity.this, "自定义对话框标题", "我是自定义对话框");
        myDialog.show();
    }


    /**
     * 普通对话框创建方法
     */
    private void rDialog() {

        AlertDialog dialog;

        dialog = new AlertDialog.Builder(this)   //绑定当前窗口
                .setTitle("普通对话框")  //设置标题
                .setMessage("这里是普通对话框界面！")  //设置提示信息
                .setIcon(R.mipmap.ic_launcher)  //设置图标
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: 确定");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: 取消");
                    }
                })
                .create();
        dialog.show();
    }

    /**
     * 单选对话框创建方法
     */
    private void radioDialog() {
        final String[] str = new String[]{"男", "女"};

        new AlertDialog.Builder(this)
                .setTitle("请选择性别")
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
                        xb = str[which];
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你选择的性别是：" + xb, Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    /**
     * 多选对话框创建方法
     */
    private void checkBoxDialog() {
        final String[] str = new String[]{"旅游", "美食", "运动", "宠物"};

        new AlertDialog.Builder(this)
                .setTitle("请添加兴趣爱好！")
                .setIcon(R.mipmap.ic_launcher)
                .setMultiChoiceItems(str, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
                        if (isChecked) {
                            intrest.add(str[which]);
                        } else {
                            intrest.remove(str[which]);
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String checkedItems = "";
                        for (String temp :
                                intrest) {
                            checkedItems += temp + " ";
                        }
                        Log.i(TAG, "选择了：" + checkedItems);
                        intrest.clear();
                    }
                })
                .show();
    }

    /**
     * 显示基本进度条对话框
     *
     * @param view
     */
    public void showBasicProgressDialog(View view) {
        basicProgressDialog();
    }


    /**
     * 基本进度条对话框创建方法
     */
    private void basicProgressDialog() {
        final ProgressDialog progressDialog;

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("基本进度条对话框");
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setMessage("正在下载，请稍后...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
    }




    @Override
    protected Dialog onCreateDialog(int id) {
        Log.i(TAG, "onCreateDialog: 被调用");
        switch (id) {
            case PROGRESS_DIALOG:
                virtualProgressDialog = new ProgressDialog(this);
                virtualProgressDialog.setMax(100);
                virtualProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                virtualProgressDialog.setTitle("模拟进度条对话框");
                break;
        }
        return virtualProgressDialog;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        Log.i(TAG, "onPrepareDialog: 被调用");
        switch (id) {
            case PROGRESS_DIALOG:
                virtualProgressDialog.incrementProgressBy(-virtualProgressDialog.getProgress());
                new Thread() {
                    @Override
                    public void run() {
                        for (int i = 0; i <= 100; i++) {
                            handler.sendEmptyMessage(INCREASE);
                            if (virtualProgressDialog.getProgress() >= 100) {
                                break;
                            }
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
                break;
        }
    }
}
